﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;

namespace tinymce3x
{
	public partial class upload : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			string fname = Request.Form["UserName"];
			int len = Request.ContentLength;

			if (Request.Files.Count > 0)
			{
				HttpPostedFile file = Request.Files.Get(0);
				//取原始图片：http://www.ncmem.com/doc/view.aspx?id=06e3a01a60a84f8e968dd49a5a7c66e8
				var ori = Request.Files.Get("original");
				string pathAbs = Server.MapPath("/upload");
				//自动创建文件夹
				if (!Directory.Exists(pathAbs)) Directory.CreateDirectory(pathAbs);

				string ext = Path.GetExtension(file.FileName).ToLower();
				string nameLoc = Path.GetFileName(file.FileName);
				var oriName = Path.GetFileName(ori.FileName);
				//防止用户上传非图片文件
				if (ext == ".gif"
					|| ext == ".jpg"
					|| ext == ".jpeg"
					|| ext == ".bmp"
					|| ext == ".png")
				{
					string pathSvr = Path.Combine(pathAbs, nameLoc);
					string oriSvr = Path.Combine(pathAbs, oriName);

					file.SaveAs(pathSvr);
					ori.SaveAs(oriSvr);
					//最后将新生成的文件名称发给客户端
					Response.Write("/upload/" + nameLoc);
				}
			}
		}
	}
}
